import 'package:flutter/material.dart';
import 'package:mi_mang_flutter/provider/locale_provider.dart';
import 'package:mi_mang_flutter/utils/locale_util.dart';
import 'package:provider/provider.dart';

class SettingLanguagePage extends StatefulWidget {
  SettingLanguagePage({Key? key}) : super(key: key);

  @override
  _SettingLanguagePageState createState() => _SettingLanguagePageState();
}

class _SettingLanguagePageState extends State<SettingLanguagePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(i18n(context).language),
        centerTitle: true,
      ),
      body: _buildBody(),
    );
  }

  Widget _buildBody() {
    return ListView.builder(
      shrinkWrap: true,
      itemCount: LocaleProvider.localeValueList.length,
      itemBuilder: (context, index) {
        var model = Provider.of<LocaleProvider>(context);
        return Material(
          child: Column(
            children: <Widget>[
              RadioListTile(
                value: index,
                groupValue: model.localeIndex,
                title: Text(
                  LocaleProvider.localeName(index, context),
                ),
                onChanged: (dynamic index) {
                  model.switchLocale(index);
                },
                controlAffinity: ListTileControlAffinity.trailing,
              ),
              index != LocaleProvider.localeValueList.length - 1
                  ? Divider()
                  : SizedBox.shrink(),
            ],
          ),
        );
      },
    );
  }
}
